package com.woniuxy.util;

import com.woniuxy.service.dto.StoreInfoForEsDto;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
public class EsUtil {
    @Autowired
    private  ElasticsearchRestTemplate template;


    public  StoreInfoForEsDto findOneStore(Long storeId) {
        Criteria criteria = Criteria.where(new SimpleField("id")).is(storeId);
        CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);



        List<String > list = new ArrayList<>();
        list.add(storeId.toString());
        NativeSearchQuery nsq = new NativeSearchQueryBuilder().withIds(list).build();
        SearchHits<StoreInfoForEsDto> search = template.search(criteriaQuery, StoreInfoForEsDto.class);
        List<SearchHit<StoreInfoForEsDto>> searchHits = search.getSearchHits();
        return searchHits.get(0).getContent();
    }
}
